
# REQUIRES: Data/Roper Data.csv

# NOTE: Uses code from Stargazer Package Updates.R instead of the published version of Stargazer. See readme.txt.

################# Dependencies #################
# source("Load Packages.R")
# source("Analysis - Roper/Cleaning.R")

################# Table S7 - Predictors of US Public Support for Withdrawal, 1946-2021 #################
# With question features and percent of options that were for withdrawal
OLS_q <- lm(percwithdraw ~  bEnemyVictory + bDeescalationOption + bOnesidedquestion + responseoptions,
            data = roper)

# With situation features
OLS_s <- lm(percwithdraw ~  logcas + FPR + Vietnam + Gulf_War + Afghanistan + Iraq, 
            data = roper)

# With question and situation features
OLS_qs <- lm(percwithdraw ~  bEnemyVictory + bDeescalationOption + bOnesidedquestion + responseoptions 
             + logcas + FPR + Vietnam + Gulf_War + Afghanistan + Iraq, 
             data = roper)

# Table 1 with clustered standard errors by survey ID
cOLS_q <- coeftest(OLS_q, vcov = vcovCL, df = 174, cluster = ~Roper)
cOLS_s <- coeftest(OLS_s, vcov = vcovCL, df = 174, cluster = ~Roper)
cOLS_qs <- coeftest(OLS_qs, vcov = vcovCL, df = 174, cluster = ~Roper)

output <- stargazer(cOLS_q, cOLS_s, cOLS_qs, title="Table A7: Replication of Observational Results with SEs Clustered by Poll",
                    align=TRUE, dep.var.labels=c(""), dep.var.caption="",
                    column.labels=c(" ","",""),
                    digits = 3, digits.extra = 0,
                    notes = "P-value shown in parentheses below coefficient", 
                    notes.append = FALSE,
                    report = "vc*p",
                    covariate.labels=c("Enemy Victory Framing", "Troop Reduction Option", "One-sided Question",
                                       "No. of Response Options", "Log Cumulative Casualties", "Foreign Policy Restraint",
                                       "Vietnam", "Gulf War", "Afghanistan", "Iraq", "Constant"),
                    omit.stat=c("LL","ser","f","adj.rsq"), no.space=TRUE, notes.align="c", out = "Plots/Table S7 - Predictors of US Public Support for Withdrawal, 1946-2021_R.html")

webshot("Plots/Table S7 - Predictors of US Public Support for Withdrawal, 1946-2021_R.html", "Plots/Table S7 - Predictors of US Public Support for Withdrawal, 1946-2021_R.png", zoom=5, vwidth=580, vheight=600)
# Note: Add the independent variable groups in Word and remove "_R" from the file name
# webshot("Plots/Table S7 - Predictors of US Public Support for Withdrawal, 1946-2021.html", "Plots/Table S7 - Predictors of US Public Support for Withdrawal, 1946-2021.png", zoom=5, vwidth=500, vheight=580)

rm(OLS_q, OLS_qs, OLS_s, cOLS_q, cOLS_qs, cOLS_s, output)
